package com.yizhi.student.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yizhi.common.annotation.Log;
import com.yizhi.common.controller.BaseController;
import com.yizhi.common.utils.PageUtils;
import com.yizhi.common.utils.R;
import com.yizhi.student.domain.StudentInfoDO;
import com.yizhi.student.service.ClassService;
import com.yizhi.student.service.CollegeService;
import com.yizhi.student.service.MajorService;
import com.yizhi.student.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 生基础信息表
 */

@Controller
@RequestMapping("/student/studentInfo")
@Api(value = "学生信息控制类")
public class StudentInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(StudentInfoController.class);

	@Autowired
	private StudentInfoService studentInfoService;

	@Resource
	private CollegeService collegeService;
	@Resource
	private MajorService majorService;
	@Resource
	private ClassService classService;

	/**
	 * 学生信息储存
	 */
	@ApiOperation("学生信息储存")
	@Log("学生信息保存")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("student:studentInfo:add")
	public R save(StudentInfoDO studentInfoDO) {
		// 此处应当验证 学院/专业/班级信息
		int save = studentInfoService.save(studentInfoDO);
		LOGGER.info("插入姓名:{},身份证号:{}", studentInfoDO.getStudentName(), studentInfoDO.getCertify());
		if (save > 0) {
			return R.ok("新增" + studentInfoDO.getStudentName() + "学生成功");
		}
		if (save == -1) {
			return R.error("该身份证号已存在");
		}
		if (studentInfoDO.getIsstate() == -1) {
			return R.error("在校状态异常");
		}
		return R.error("系统异常");
	}

	/**
	 * 可分页 查询
	 */
	@ApiOperation("学生信息分页查询")
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("student:studentInfo:studentInfo")
	//http://localhost:8080/student/studentInfo/list?currPage=1&pageSize=10&name=&tocollegeId=&tomajorId=&classId=
	public PageUtils list(@RequestParam Map<String, Object> params) {
		params.entrySet().removeIf(entry -> StrUtil.isBlankIfStr(entry.getValue()));
		// 总记录数
		int total = studentInfoService.count(params);
		if (total == 0) {
			return new PageUtils(new ArrayList<>(), 0);
		}
		// 页大小
		int pageSize = Integer.parseInt(params.get("pageSize").toString());
		// 当前行号
		int currPage = Integer.parseInt(params.get("currPage").toString());
		Integer pageNow = (currPage - 1) * pageSize;
		params.put("pageNow", pageNow);
		// 组装pageUtils
		List<StudentInfoDO> list = studentInfoService.list(params);
		PageUtils pageUtils = new PageUtils(list, total);
		pageUtils.setCurrPage(currPage);
		pageUtils.setPageSize(Integer.parseInt(params.get("pageSize").toString()));
		int totalPage = (int) Math.ceil((double) total / pageSize);
		pageUtils.setTotalPages(totalPage);
		pageUtils.setCode(0);
		LOGGER.info("分页查询学生信息,页号:{},页大小:{}", currPage, pageSize);
		LOGGER.info("分页查询参数:{}", params);
		return pageUtils;
	}


	/**
	 * 修改
	 */
	@ApiOperation("学生基础信息表修改")
	@Log("学生基础信息表修改")
	@ResponseBody
	@PostMapping("/update")
	@RequiresPermissions("student:studentInfo:edit")
	public R update(StudentInfoDO studentInfo) {
		// 此处应当验证 学院/专业/班级信息
		if (ObjectUtil.isNull(studentInfo.getId())) {
			throw new RuntimeException("操作非法");
		}
		if (studentInfo.getIsstate() == -1) {
			return R.error("在校状态异常");
		}
		int save = studentInfoService.update(studentInfo);
		LOGGER.info("更新学生{}数据", studentInfo.getStudentName());
		if (save > 0) {
			return R.ok();
		} else {
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@ApiOperation("学生基础信息表删除")
	@Log("学生基础信息表删除")
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("student:studentInfo:remove")
	public R remove(Integer id) {
		int remove = studentInfoService.remove(id);
		LOGGER.info("删除id为{}学生数据", id);
		if (remove > 0) {
			return R.ok("删除成功");
		}
		throw new RuntimeException("系统异常");
	}

	/**
	 * 批量删除
	 */
	@ApiOperation("学生基础信息表批量删除")
	@Log("学生基础信息表批量删除")
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("student:studentInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		int remove = studentInfoService.batchRemove(ids);
		LOGGER.info("删除id为{}学生数据", Arrays.toString(ids));
		if (remove > 0) {
			return R.ok("删除成功");
		}
		throw new RuntimeException("系统异常");
	}


	//前后端不分离 客户端 -> 控制器-> 定位视图

	/**
	 * 学生管理 点击Tab标签 forward页面
	 */
	@GetMapping()
	@RequiresPermissions("student:studentInfo:studentInfo")
	String StudentInfo() {
		return "student/studentInfo/studentInfo";
	}

	/**
	 * 更新功能 弹出View定位
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("student:studentInfo:edit")
	String edit(@PathVariable("id") Integer id, Model model) {
		StudentInfoDO studentInfo = studentInfoService.get(id);
		model.addAttribute("studentInfo", studentInfo);
		return "student/studentInfo/edit";
	}

	/**
	 * 学生管理 添加学生弹出 View
	 */
	@GetMapping("/add")
	@RequiresPermissions("student:studentInfo:add")
	String add() {
		return "student/studentInfo/add";
	}

}//end class
